package com.gable.common.filter;

import com.gable.common.util.CacheData;
import com.gable.common.util.StringUtil;
import com.gable.exhibition.model.User;
import com.gable.exhibition.util.MessageUtil;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Lesson on 2017/12/28.
 */
@Component("authorizationFilter")
public class AuthorizationFilter implements Filter{

    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    HttpServletRequest request;

    private CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;



        String path = request.getServletPath();

        if(path.indexOf("hospital/findNeighPosition") > -1 || path.indexOf("repair/addRepair") > -1 || path.indexOf("fileUpload/fileUploadForWeChat") > -1 || path.indexOf("user/getOpenId") > -1){
            filterChain.doFilter(request,response);
            return;
        }

        if(path.indexOf("fileUpload") > -1){
            MultipartHttpServletRequest multiReq = multipartResolver.resolveMultipart(request);
            request = multiReq;
        }
        Long userId = null;
        if(!StringUtil.isEmpty(request.getParameter("userId"))){
            userId = Long.parseLong(request.getParameter("userId"));
        }

        User user = CacheData.getSessionUser(userId,1);

        logger.info(request.getMethod());

        if(user == null){
            request.setAttribute("type", "1");
            if( request.getMethod().equals("POST")){
                request.getRequestDispatcher("../user/loginErrorByPost").forward(request,response);
            }else{
                request.getRequestDispatcher("../user/loginErrorByGet").forward(request,response);
            }

            return;
        }

        logger.debug("用户secretKey ========= " + user.getSecretKey());
        logger.debug("appsecretKey==========" + request.getParameter("secretKey"));

        String secretKey = null;
        if(!StringUtil.isEmpty(request.getParameter("secretKey"))){
            secretKey = request.getParameter("secretKey");

            if(!user.getSecretKey().equals(secretKey)){
                request.setAttribute("type", "2");
                request.getRequestDispatcher("../user/loginErrorByPost").forward(request,response);
                return;
            }
        }

        filterChain.doFilter(request,response);

    }

    @Override
    public void destroy() {

    }
}
